<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width:  100%;
            height: 100px;
            background-color: skyblue;
            font-size: 50px;
            line-height: 100px;
            text-align: center;
            color: #fff;
            /* 标准流 */
            position: static;
            top: 0;
            left: 0;
        }
        button{
            background-color: pink;
            width: 80px;
            height: 30px;

            position: fixed;
            right: 20px;
            bottom: 20px;

            display: none;
        }
        body{
            height: 3000px;
        }
    </style>
</head>
<body>
    <div>通栏盒子</div>
    <button>返回顶部</button>
    <script>
        //获取两个元素
        var box = document.querySelector("div")
        var btn = document.querySelector("button")
        
        //下拉滚动条
        window.onscroll = function(){
            var stop = window.document.documentElement.scrollTop || document.body.scrollTop
            if(stop > 500){
                //将他改成固定定位
                box.setAttribute("style","position: fixed")
                //让btn按钮显示
                btn.style.display = "block"
            }else{
                //将他改成static
                box.setAttribute("style","position: static")
                btn.style.display = "none"
            }
        }
        btn.onclick = function(){
            window.scrollTo({
                top:0,
                behavior:"smooth"
            })
        }
    </script>
</body>
</html>